var beiMiCommon = require("BeiMiCommon");
var clubArr = [];
var roomArr = [];
cc.Class({
  extends: beiMiCommon,

  properties: {
    manage: {
      default: null,
      type: cc.Prefab
    },
    chat: {
      default: null,
      type: cc.Prefab
    },
    joinClub: {
      default: null,
      type: cc.Prefab
    },
    createClub: {
      default: null,
      type: cc.Prefab
    },
    playTab: 'club1',
    play: {
      default: null,
      type: cc.Node
    },
    memberList: {
      default: null,
      type: cc.Prefab
    },
    clubname: {
      default: null,
      type: cc.Label
    },
    clubData: {
      default: null,
      type: cc.Prefab
    },
    roomData: {
      default: null,
      type: cc.Prefab
    },
    roomList: {
      default: null,
      type: cc.Node
    }
  },
  onLoad: function () {
    this.group = new Array();
    this.room = new Array();
    
    this.getClubs();
    //监听点击俱乐部
    this.clubClick();

  },
  getClubs:function(){
    let self = this;
    var param = "";
    let socket = this.socket();
    socket.emit("getclubs", param);
    cc.beimi.socket.on("getclubs", function (result) {
      console.log(JSON.parse(result))
      var res = JSON.parse(result);
      clubArr = res.sxClubUsers;

      //俱乐部名称
      // var node = cc.find("Canvas/index");
      // var clubname = node.getChildByName("base1").getChildByName("left").getChildByName("clubname");
      // var label = clubname.getComponent(cc.Label);
      if (clubArr && clubArr[0]) {
        self.curClubId = clubArr[0].showid;
        cc.beimi.clubId = clubArr[0].showid;
        self.clubname.string = (clubArr[0].clubname || "俱乐部名称") + " ID " + clubArr[0].showid;

        //获取房间列表
        self.getRooms(clubArr[0].clubid)

      }

      self.play.removeAllChildren();
      //俱乐部列表
      for (var i = 0, len = clubArr.length; i < len; i++) {
        //克隆一个预制资源节点
        let group = cc.instantiate(self.clubData);
        let clubGroup = group.getComponent("clubData");
        clubGroup.init(clubArr[i], self.curClubId);
        //添加到group数组
        self.group.push(clubGroup);
        //挂载到节点
        group.parent = self.play;
      }

    });
    
  },
  getRooms:function(clubid){
    var self=this;
    let socket = this.socket();
    socket.emit("getrooms", {
      clubid: clubid
    });
    console.log(clubid)
    cc.beimi.socket.on("getrooms", function (result) {
      var res = JSON.parse(result)
      console.log(res.gameRooms)
      roomArr = res.gameRooms;
      self.initRoom();
    })
  },
  clubClick: function () {
    var self=this;
    self.node.on('clubClick', function (e) {
      console.log(e.detail)
      cc.beimi.clubId = e.detail.id;
      self.clubname.string = e.detail.name + " ID " + e.detail.id;
      var playTab = e.target.parent.parent.children;
      var length = e.target.parent.parent.children.length;
      for (var i = 0; i < length; i++) {
        playTab[i].getChildByName('back').active = false;
      }
      e.target.parent.getChildByName("back").active = true;
      //console.log(e.target.parent.parent.children)
      self.getRooms(e.detail.realId);
      e.stopPropagation();
    });
  },
  initRoom: function () {
    this.roomList.removeAllChildren();
    for (var j = 0, jlen = roomArr.length; j < jlen; j++) {
      let room = cc.instantiate(this.roomData);
      let roomGroup = room.getComponent("roomData");
      roomGroup.init(roomArr[j]);
      this.room.push(roomGroup);
      room.parent = this.roomList;
    }
  },
  onManageClick: function () {
    cc.beimi.openwin2 = cc.instantiate(this.manage);
    cc.beimi.openwin2.parent = cc.find('Canvas');
  },
  onChatClick: function () {
    cc.beimi.openwin2 = cc.instantiate(this.chat);
    cc.beimi.openwin2.parent = cc.find('Canvas');
  },
  onJoinClubClick: function () {
    cc.beimi.openwin2 = cc.instantiate(this.joinClub);
    cc.beimi.openwin2.parent = cc.find('Canvas');
  },
  onCreateClubClick: function () {
    cc.beimi.openwin2 = cc.instantiate(this.createClub);
    cc.beimi.openwin2.parent = cc.find('Canvas');
  },
  initTab: function (tabs, activeTab) {
    tabs.map(function (item) {
      if (activeTab == item.name) {
        item.getChildByName('back').active = true;
      } else {
        item.getChildByName('back').active = false;
      }
    })
  },
  playTabClick: function (e) {
    if (this.playTab != e.currentTarget.name) {
      this.playTab = e.currentTarget.name;
      this.initTab(this.play.children, this.playTab);
    }
  },
  onMemberListClick: function () {
    cc.beimi.openwin2 = cc.instantiate(this.memberList);
    cc.beimi.openwin2.parent = cc.find('Canvas');
  },
});
